<template>
    <div>
        <section class="hero">
            <el-carousel :interval="4000" arrow="always">
                <el-carousel-item v-for="(item, index) in carouselItems" :key="index">
                    <div class="carousel-content">
                        <img
                            :src="item.image"
                            alt="Carousel Image"
                            class="carousel-image"
                        >
                        <div class="carousel-text">
                            <h2>{{ item.title }}</h2>
                            <p>{{ item.description }}</p>
                            <a href="#" class="btn">{{ item.buttonText }}</a>
                        </div>
                    </div>
                </el-carousel-item>
            </el-carousel>
        </section>
        <section class="courses">
            <h2 class="course-title">热门课程</h2>
            <div class="course-list">
                <div v-for="course in courses" :key="course.id" class="course" @click="toPeriod(course.id)">
                    <img
                        :src="serverIp + course.url"
                        :alt="course.name"
                        @error="handleImageError"
                    >
                    <div class="btn">{{ course.name }}</div>
                </div>
            </div>
        </section>
    </div>
</template>

<script setup>
    import { ref } from 'vue';
    import lunbotu1 from '~/assets/lunbotu1.png';
    import lunbotu2 from '~/assets/lunbotu2.png';
    import lunbotu3 from '~/assets/lunbotu3.png';
    import nomore from '~/assets/nomore.png'
    import {useRouter} from "vue-router";
    import request from "~/utils/request.js";
    import { onMounted } from 'vue'
    import { serverIp } from "~/utils/config.js"; // 引入配置文件中的serverIp变量
    import { useStore } from 'vuex' // 引入useStore钩子函数
    const store = useStore() // 调用useStore钩子函数

    const router = useRouter()
    const courses = ref([]);
    const userId = ref()
    const nomoreimg = ref(nomore)

    onMounted(() => {
        // 获取用户id
        userId.value = store.state.user.id;

        const getCourse = () => {
        request.get("/course/stuPage", {
                    params: {
                        pageNum: 1,
                        pageSize: 4,
                        userId: userId.value,
                    }
                }).then(res => {
                    if (res.code === "200") {
                        courses.value = res.data.records
                    }
                })
        }
        getCourse()
    })
    const carouselItems = ref([
        {
            title: '开启你的学习之旅',
            description: '海量优质课程，助你提升自我',
            buttonText: '立即开始',
            image: lunbotu1
        },
        {
            title: '探索新的知识领域',
            description: '丰富的课程资源，满足你的学习需求',
            buttonText: '马上探索',
            image: lunbotu2
        },
        {
            title: '提升你的技能水平',
            description: '专业的课程指导，让你快速成长',
            buttonText: '开始提升',
            image: lunbotu3
        }
    ]);
    const toPeriod = (scope) => {
      router.push({path: "/course/period", query: {courseId: scope}})
    };
    const handleImageError = (e) => {
        e.target.src = nomore  // 直接使用导入的nomore路径
        e.target.classList.add('img-error') // 可选：添加错误样式
    }
</script>

<style scoped>
    body {
        font-family: Arial, sans-serif;
        margin: 0;
        padding: 0;
    }

    /* 去除 header 和轮播图之间的默认边距 */

    .header-container {
        display: flex;
        justify-content: space-between;
        align-items: center;
        padding: 0 50px; /* 加宽头部 */
        max-width: 1400px; /* 最大宽度 */
        margin: 0 auto; /* 居中 */
    }

    header {
        background-color: #333;
        color: white;
        text-align: center;
        padding: 10px 0;
    }

    nav ul {
        list-style-type: none;
        margin: 0;
        padding: 0;
        display: flex;
        margin-left: 50px; /* 导航标签移到中间靠左 */
    }

    nav ul li {
        margin: 0 15px;
    }

    .nav-link {
        color: white;
        text-decoration: none;
        font-size: 18px;
    }

    .hero {
        margin-top: 0;
        padding-top: 0;
        background-size: cover;
        background-position: center;
        text-align: center;
        color: white;
    }

    .carousel-content {
        position: relative;
        display: flex;
        align-items: center;
        justify-content: center;
        height: 100%;
    }

    .carousel-image {
        width: 100%;
        height: auto;
        object-fit: cover;
    }

    .carousel-text {
        position: absolute;
        top: 50%;
        left: 50%;
        transform: translate(-50%, -50%);
        background-color: rgba(0, 0, 0, 0.5);
        padding: 20px;
        border-radius: 5px;
    }

    .carousel-text h2 {
        font-size: 36px;
    }

    .carousel-text p {
        font-size: 18px;
        margin-bottom: 20px;
    }

    .course {
        width: calc(25% - 32px); /* 调整间距 */
        margin: 16px;
        border: 1px solid #eee;
        border-radius: 12px;
        padding: 16px;
        box-shadow: 0 2px 8px rgba(0,0,0,0.08);
        transition: all 0.3s ease;
        background: white;
    }

    .course:hover {
        transform: translateY(-4px);
        box-shadow: 0 6px 16px rgba(0,0,0,0.12);
    }

    /* 响应式调整 */
    @media (max-width: 1200px) {
        .course { width: calc(33.3% - 32px); }
    }
    @media (max-width: 768px) {
        .carousel-image {
            width: auto;
            height: 100%;
            object-fit: cover;
        }
        .carousel-text {
            position: absolute;
            top: 50%;
            left: 50%;
            transform: translate(-50%, -50%);
            background-color: rgba(0, 0, 0, 0.5);
            padding: 20px;
            border-radius: 5px;
        }

        .carousel-text h2 {
            font-size: 18px;
        }

        .carousel-text p {
            font-size: 15px;
            margin-bottom: 10px;
        }
        .course { width: 100%; }
    }

    .btn {
        margin-top: 20px;
        display: inline-block;
        background-color: #ff6600;
        color: white;
        padding: 10px 20px;
        text-decoration: none;
        border-radius: 5px;
    }

    .courses {
        padding: 30px;
        text-align: center;
    }

    .course-title {
        font-size: 32px;
        color: #333;
        text-transform: uppercase; /* 字母大写 */
        letter-spacing: 2px; /* 字母间距 */
    }

    .course-list {
        display: flex;
        flex-wrap: wrap;
        justify-content: center;
    }

    .img-error {
        background: #f5f7fa url('~/assets/nomore.png') no-repeat center/50%;
        border: 1px dashed #ddd;
    }

    /* 移动端适配 */
    @media (max-width: 768px) {
        .img-error {
            height: 150px;
        }
    }

    /* 原有课程图片样式新增 */
    .course img {
        width: 100%;
        height: 200px; /* 固定高度 */
        object-fit: cover; /* 保持图片比例 */
        border-radius: 5px;
        margin-bottom: 10px;
    }

    footer {
        background-color: #333;
        color: white;
        text-align: center;
        padding: 10px;
    }
</style>
